package com.lin.learning.questionmanagement.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.learning.questionmanagement.dto.AddTeacherDto;
import com.lin.learning.questionmanagement.dto.UpdateTeacherDto;
import com.lin.learning.questionmanagement.mapper.TeacherMapper;
import com.lin.learning.questionmanagement.service.TeacherService;
import com.lin.learning.questionmanagement.utils.BeanCopyUtils;
import com.lin.learning.questionmanagement.vo.TeacherVo;
import com.our.learning.infrastructure.po.Teacher;
import com.our.learning.infrastructure.po.User;
import com.our.learning.infrastructure.pojo.ResponseResult;
import com.studentmanagement.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private  TeacherMapper teacherMapper;

/*    @Override
    public ResponseResult teacherList(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Teacher> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Teacher::getId);
        Page<Teacher> page=new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);
        List<Teacher> list=page.getRecords();
        PageVo pageVo=new PageVo(list,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }*/

    @Override
    public ResponseResult getTeacherByName(String name) {
        TeacherVo teacher= teacherMapper.selectByTeacher_name(name);
        return ResponseResult.okResult(teacher);
    }

    @Override
    public ResponseResult addTeacher(AddTeacherDto addTeacherDto) {
        User user=new User(addTeacherDto.getAccount(),addTeacherDto.getPassword(),addTeacherDto.getType());
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getAccount,addTeacherDto.getAccount());
        if (userMapper.selectOne(lambdaQueryWrapper)!=null){
            return ResponseResult.errorResult(511,"该account已存在");
        }
        userMapper.insert(user);
        Teacher teacher=new Teacher(user.getId(),addTeacherDto.getName(),addTeacherDto.getClassId(),addTeacherDto.getPhone());
        save(teacher);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTeacher(Integer id) {
        if (teacherMapper.selectByTeacher_id(id)==null){
            // TODO: 2023-06-21 提示没有老师
            return ResponseResult.errorResult(506,"該教師不存在");
        }
        teacherMapper.deleteByTeacher_id(id);
        userMapper.deleteById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateTeacher(UpdateTeacherDto updateTeacherDto) {
        Teacher teacher=BeanCopyUtils.copyBean(updateTeacherDto,Teacher.class);
        teacherMapper.updateByTeacher_id(teacher.getTeacher_id(),teacher.getName(),teacher.getClass_id(),teacher.getPhone());
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTeacherById(Integer id) {
        TeacherVo teacher= teacherMapper.selectByTeacher_id(id);
        return ResponseResult.okResult(teacher);
    }
}
